package co.fitstart.mobile.web.controller.profile;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.wechat.web.interceptor.WechatJsConfig;

import co.fitstart.entity.coupon.Coupon;
import co.fitstart.entity.user.Student;
import co.fitstart.entity.user.User;
import co.fitstart.entity.user.agent.Agent;
import co.fitstart.mobile.service.CouponService;
import co.fitstart.mobile.service.UserAgentService;
import co.fitstart.mobile.service.UserService;
import co.fitstart.mobile.web.interceptor.MobileDetect;
import co.fitstart.mobile.web.interceptor.Router;
import co.fitstart.mobile.web.interceptor.WechatAuthority;
import co.fitstart.mobile.web.resolver.Secure;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/profile")
public class ProfileController {
    
    @Autowired
    private CouponService couponService;
    
    @Autowired
    private UserAgentService userAgentService;
    
    @Autowired
    private UserService userService;
    
    /**
     * Profile home page with course & order & profile info
     * 
     * @param user
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView index(@Secure User user) {
        
        ModelAndView modelAndView = new ModelAndView("profile/profile-index");
        modelAndView.addObject("user", user);
        
        Agent agent = userAgentService.get(user);
        modelAndView.addObject("agent", agent);
        
        Student student = userService.getStudent(user);
        modelAndView.addObject("student", student);
        
        return modelAndView;
    }
    
    /**
     * Profile number card page
     * 
     * @param user
     * @return
     */
    @RequestMapping(value = "/number-card", method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView numberCard(@Secure User user) {
        
        ModelAndView modelAndView = new ModelAndView("profile/profile-number-card");
        modelAndView.addObject("user", user);
        
        Date effectiveEnd = userService.latestEffectiveDate(user);
        modelAndView.addObject("effectiveEnd", effectiveEnd);
        
        return modelAndView;
    }
    
    /**
     * Profile coupon page
     * 
     * @param user
     * @return
     */
    @RequestMapping("/coupons")
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView coupons(@Secure User user) {
        
        ModelAndView modelAndView = new ModelAndView("profile/profile-coupon-list");
        
        List<Coupon> coupons = couponService.listCoupon(user);
        modelAndView.addObject("coupons", coupons);
        
        return modelAndView;
    }
    
    @RequestMapping("/coupon/{couponId}")
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView coupon(@Secure User user,
                               @PathVariable("couponId") Long couponId) {
        Coupon coupon = couponService.load(Coupon.class, couponId);
        
        ModelAndView modelAndView = new ModelAndView("profile/profile-coupon-view");

        if(coupon == null || !user.getId().equals(coupon.getUserId()) || !coupon.getStatus().isClaimed() || coupon.getRestDay() <= 0) {
            coupon = null;
        }
        
        if(coupon.getOriginalUserId() != null) {
            User originalUser = userService.load(User.class, coupon.getOriginalUserId());
            if(originalUser != null) {
                modelAndView.addObject("originalUserName", originalUser.getName() == null ? originalUser.getMobile() : originalUser.getName());
            }
        }

        modelAndView.addObject("coupon", coupon);
        
        return modelAndView;
    }
    
    /**
     * Profile coupon page
     * 
     * @param user
     * @return
     */
    @RequestMapping("/credits")
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView credits(@Secure User user) {
        
        ModelAndView modelAndView = new ModelAndView("profile/profile-credit-list");
        
        return modelAndView;
    }

}
